package src;

import java.io.IOException;
import java.util.Vector;

import lucene.LuceneConstants;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Searcher;

public class SearchCluster {

	/**
	 * @param args
	 */
	IndexReader reader=null;
	Searcher searcher;
	Analyzer qanalyzer;
	QueryParser parser;
	public SearchCluster(){
		try{
			reader = IndexReader.open(LuceneConstants.clusterindexdir);
			searcher= new IndexSearcher(reader);
			qanalyzer = new  StandardAnalyzer();
			parser= new QueryParser("k1",qanalyzer);
		}
		catch(Exception e){
			e.printStackTrace();
			System.exit(-1);
		}
	}
	public Vector search(String querystr){
		Query query= null;
		Vector vec = new Vector();
		
			try {
				query= parser.parse(querystr);
				Hits hits= searcher.search(query);
				for(int i=0; i<hits.length(); i++){
					Document doc= hits.doc(i);
					float score=hits.score(i);
					
					//Field variations = doc.getField("variations");
					Field head = doc.getField("h1"); 
					vec.addElement(head.stringValue());
				}
			} catch (ParseException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return vec;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SearchCluster sc = new SearchCluster();
		String querystr = "blk";
		Vector hitdocs = sc.search(querystr);
		System.out.println(hitdocs.size());
		for(int i=0; i< hitdocs.size(); i++){
			System.out.println(hitdocs.get(i));
		}
	}
}
